home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 June: Reference Library / Dev.CD Jun 95 / Dev.CD Jun 95.toast / What's New? / Sample Code / MoreFiles 1.3 / MoreFilesExtras.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  64.1 KB  |  2,182 lines  |  [TEXT/MMCC]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    A collection of useful high-level File Manager routines.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support
  7. **
  8. **    File:        MoreFilesExtras.c
  9. **
  10. **    Copyright © 1992-1995 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include <Types.h>
  23. #include <Errors.h>
  24. #include <Files.h>
  25. #include <Devices.h>
  26. #include <Folders.h>
  27. #include "MoreFiles.h"
  28. #include "MoreFilesExtras.h"
  29.  
  30. /*****************************************************************************/
  31.  
  32. /* local data structures */
  33.  
  34. /* The DeleteEnumGlobals structure is used to minimize the amount of
  35. ** stack space used when recursively calling DeleteLevel and to hold
  36. ** global information that might be needed at any time. */
  37.  
  38. #if GENERATINGPOWERPC
  39. #pragma options align=mac68k
  40. #endif
  41. struct DeleteEnumGlobals
  42. {
  43.     OSErr            error;                /* temporary holder of results - saves 2 bytes of stack each level */
  44.     Str63            itemName;            /* the name of the current item */
  45.     UniversalFMPB    myPB;                /* the parameter block used for PBGetCatInfo calls */
  46. };
  47. #if GENERATINGPOWERPC
  48. #pragma options align=reset
  49. #endif
  50.  
  51. typedef struct DeleteEnumGlobals DeleteEnumGlobals;
  52. typedef DeleteEnumGlobals *DeleteEnumGlobalsPtr;
  53.  
  54. /*****************************************************************************/
  55.  
  56. pascal    Ptr    GetTempBuffer(long buffReqSize,
  57.                           long *buffActSize)
  58. {
  59.     enum
  60.     {
  61.         kSlopMemory = 0x00008000    /* 32K - Amount of free memory to leave when allocating buffers */
  62.     };
  63.     Ptr    tempPtr;
  64.     
  65.     /* Make request a multiple of 1024 bytes */
  66.     buffReqSize = buffReqSize & 0xfffffc00;
  67.     
  68.     if ( buffReqSize < 0x00000400 )
  69.     {
  70.         /* Request was smaller than 1024 bytes - make it 1024 */
  71.         buffReqSize = 0x00000400;
  72.     }
  73.     
  74.     /* Attempt to allocate the memory */
  75.     tempPtr = NewPtr(buffReqSize);
  76.     
  77.     /* If request failed, go to backup plan */
  78.     if ( (tempPtr == NULL) && (buffReqSize > 0x00000400) )
  79.     {
  80.         /*
  81.         **    Try to get largest 1024-byte block available
  82.         **    leaving some slop for the toolbox if possible
  83.         */
  84.         long freeMemory = (FreeMem() - kSlopMemory) & 0xfffffc00;
  85.         
  86.         buffReqSize = MaxBlock() & 0xfffffc00;
  87.         
  88.         if ( buffReqSize > freeMemory )
  89.         {
  90.             buffReqSize = freeMemory;
  91.         }
  92.         
  93.         if ( buffReqSize == 0 )
  94.         {
  95.             buffReqSize = 0x00000400;
  96.         }
  97.         
  98.         tempPtr = NewPtr(buffReqSize);
  99.     }
  100.     
  101.     /* Return bytes allocated */
  102.     if ( tempPtr != NULL )
  103.     {
  104.         *buffActSize = buffReqSize;
  105.     }
  106.     else
  107.     {
  108.         *buffActSize = 0;
  109.     }
  110.     
  111.     return ( tempPtr );
  112. }
  113.  
  114. /*****************************************************************************/
  115.  
  116. pascal    OSErr    DetermineVRefNum(StringPtr pathname,
  117.                                  short vRefNum,
  118.                                  short *realVRefNum)
  119. {
  120.     HParamBlockRec pb;
  121.     Str255 tempPathname;
  122.     OSErr error;
  123.  
  124.     pb.volumeParam.ioVRefNum = vRefNum;
  125.     if ( pathname == NULL )
  126.     {
  127.         pb.volumeParam.ioNamePtr = NULL;
  128.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  129.     }
  130.     else
  131.     {
  132.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  133.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  134.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  135.     }
  136.     error = PBHGetVInfoSync(&pb);
  137.     *realVRefNum = pb.volumeParam.ioVRefNum;
  138.     return ( error );
  139. }
  140.  
  141. /*****************************************************************************/
  142.  
  143. pascal    OSErr    HGetVInfo(short volReference,
  144.                           StringPtr volName,
  145.                           short *vRefNum,
  146.                           unsigned long *freeBytes,
  147.                           unsigned long *totalBytes)
  148. {
  149.     HParamBlockRec    pb;
  150.     unsigned long    allocationBlockSize;
  151.     unsigned short    numAllocationBlocks;
  152.     unsigned short    numFreeBlocks;
  153.     VCB                *theVCB;
  154.     Boolean            vcbFound;
  155.     OSErr            result;
  156.     
  157.     /* Use the File Manager to get the real vRefNum */
  158.     pb.volumeParam.ioVRefNum = volReference;
  159.     pb.volumeParam.ioNamePtr = volName;
  160.     pb.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only, return volume name */
  161.     result = PBHGetVInfoSync(&pb);
  162.     
  163.     if ( result == noErr )
  164.     {
  165.         /* The volume name was returned in volName (if not NULL) and */
  166.         /* we have the volume's vRefNum and allocation block size */
  167.         *vRefNum = pb.volumeParam.ioVRefNum;
  168.         allocationBlockSize = (unsigned long)pb.volumeParam.ioVAlBlkSiz;
  169.         
  170.         /* System 7.5 (and beyond) pins the number of allocation blocks and */
  171.         /* the number of free allocation blocks returned by PBHGetVInfo to */
  172.         /* a value so that when multiplied by the allocation block size, */
  173.         /* the volume will look like it has $7fffffff bytes or less. This */
  174.         /* was done so older applications that use signed math or that use */
  175.         /* the GetVInfo function (which uses signed math) will continue to work. */
  176.         /* However, the unpinned numbers (which we want) are always available */
  177.         /* in the volume's VCB so we'll get those values from the VCB if possible. */
  178.         
  179.         /* Find the volume's VCB */
  180.         vcbFound = false;
  181.         theVCB = (VCB *)(GetVCBQHdr()->qHead);
  182.         while ( (theVCB != NULL) && !vcbFound )
  183.         {
  184.             /* Check VCB signature before using VCB. Don't have to check for */
  185.             /* MFS (0xd2d7) because they can't get big enough to be pinned */
  186.             if ( theVCB->vcbSigWord == 0x4244 )
  187.             {
  188.                 if ( theVCB->vcbVRefNum == *vRefNum )
  189.                 {
  190.                     vcbFound = true;
  191.                 }
  192.             }
  193.             
  194.             if ( !vcbFound )
  195.             {
  196.                 theVCB = (VCB *)(theVCB->qLink);
  197.             }
  198.         }
  199.         
  200.         if ( theVCB != NULL )
  201.         {
  202.             /* Found a VCB we can use. Get the un-pinned number of allocation blocks */
  203.             /* and the number of free blocks from the VCB. */
  204.             numAllocationBlocks = (unsigned short)theVCB->vcbNmAlBlks;
  205.             numFreeBlocks = (unsigned short)theVCB->vcbFreeBks;
  206.         }
  207.         else
  208.         {
  209.             /* Didn't find a VCB we can use. Return the number of allocation blocks */
  210.             /* and the number of free blocks returned by PBHGetVInfoSync. */
  211.             numAllocationBlocks = (unsigned short)pb.volumeParam.ioVNmAlBlks;
  212.             numFreeBlocks = (unsigned short)pb.volumeParam.ioVFrBlk;
  213.         }
  214.         
  215.         /* Now, calculate freeBytes and totalBytes using unsigned values */
  216.         *freeBytes = numFreeBlocks * allocationBlockSize;
  217.         *totalBytes = numAllocationBlocks * allocationBlockSize;
  218.     }
  219.     
  220.     return ( result );
  221. }
  222.  
  223. /*****************************************************************************/
  224.  
  225. pascal    OSErr    CheckVolLock(StringPtr pathname,
  226.                              short vRefNum)
  227. {
  228.     HParamBlockRec pb;
  229.     Str255 tempPathname;
  230.     OSErr error;
  231.  
  232.     pb.volumeParam.ioVRefNum = vRefNum;
  233.     if ( pathname == NULL )
  234.     {
  235.         pb.volumeParam.ioNamePtr = NULL;
  236.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  237.     }
  238.     else
  239.     {
  240.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  241.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  242.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  243.     }
  244.     error = PBHGetVInfoSync(&pb);
  245.     
  246.     if ( error == noErr )
  247.     {
  248.         if ( (pb.volumeParam.ioVAtrb & 0x0080) != 0 )
  249.             error = wPrErr;        /* volume locked by hardware */
  250.         else if ( (pb.volumeParam.ioVAtrb & 0x8000) != 0 )
  251.             error = vLckdErr;    /* volume locked by software */
  252.     }
  253.     
  254.     return ( error );
  255. }
  256.  
  257. /*****************************************************************************/
  258.  
  259. pascal    OSErr GetDriverName(short driverRefNum,
  260.                             Str255 driverName)
  261. {
  262.     OSErr result;
  263.     DCtlHandle theDctl;
  264.     StringPtr thePtr;
  265.     
  266.     theDctl = GetDCtlEntry(driverRefNum);
  267.     if ( theDctl != NULL )
  268.     {
  269.         if ( (**theDctl).dCtlFlags & 0x40 )
  270.         {
  271.             /* dctlDriver is handle - dereference */
  272.             thePtr = (StringPtr)*((**theDctl).dCtlDriver);
  273.         }
  274.         else
  275.         {
  276.             /* dctlDriver is pointer */
  277.           thePtr = (StringPtr)(**theDctl).dCtlDriver;
  278.         }
  279.         thePtr += 18;    /* offset to driver name */
  280.         BlockMoveData(thePtr, driverName, thePtr[0] + 1);
  281.         result = noErr;
  282.     }
  283.     else
  284.     {
  285.         driverName[0] = 0;
  286.         result = badUnitErr;    /* bad reference number */
  287.     }
  288.     
  289.     return ( result );
  290. }
  291.  
  292. /*****************************************************************************/
  293.  
  294. pascal    OSErr    FindDrive(StringPtr pathname,
  295.                           short vRefNum,
  296.                           DrvQElPtr *driveQElementPtr)
  297. {
  298.     OSErr            result;
  299.     Str255            tempPathname;
  300.     HParamBlockRec    hPB;
  301.     short            driveNumber;
  302.     
  303.     *driveQElementPtr = NULL;
  304.     
  305.     /* First, use PBHGetVInfo to determine the volume */
  306.     hPB.volumeParam.ioVRefNum = vRefNum;
  307.     if ( pathname == NULL )
  308.     {
  309.         hPB.volumeParam.ioNamePtr = NULL;
  310.         hPB.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only */
  311.     }
  312.     else
  313.     {
  314.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  315.         hPB.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  316.         hPB.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  317.     }
  318.     result = PBHGetVInfoSync(&hPB);
  319.     if ( result == noErr )
  320.     {
  321.         /*
  322.         **    The volume can be either online, offline, or ejected. What we find in
  323.         **    ioVDrvInfo and ioVDRefNum will tell us which it is.
  324.         **    See Inside Macintosh: Files page 2-80 and the Technical Note
  325.         **    "FL 34 - VCBs and Drive Numbers : The Real Story"
  326.         **    Where we get the drive number depends on the state of the volume.
  327.         */
  328.         if ( hPB.volumeParam.ioVDrvInfo != 0 )
  329.         {
  330.             /* The volume is online and not ejected */
  331.             /* Get the drive number */
  332.             driveNumber = hPB.volumeParam.ioVDrvInfo;
  333.         }
  334.         else
  335.         {
  336.             /* The volume's is either offline or ejected */
  337.             /* in either case, the volume is NOT online */
  338.  
  339.             /* Is it ejected or just offline? */
  340.             if ( hPB.volumeParam.ioVDRefNum > 0 )
  341.             {
  342.                 /* It's ejected, the drive number is ioVDRefNum */
  343.                 driveNumber = hPB.volumeParam.ioVDRefNum;
  344.             }
  345.             else
  346.             {
  347.                 /* It's offline, the drive number is the negative of ioVDRefNum */
  348.                 driveNumber = (short)-hPB.volumeParam.ioVDRefNum;
  349.             }
  350.         }
  351.         
  352.         /* Get pointer to first element in drive queue */
  353.         *driveQElementPtr = (DrvQElPtr)(GetDrvQHdr()->qHead);
  354.         
  355.         /* Search for a matching drive number */
  356.         while ( (*driveQElementPtr != NULL) && ((*driveQElementPtr)->dQDrive != driveNumber) )
  357.         {
  358.             *driveQElementPtr = (DrvQElPtr)(*driveQElementPtr)->qLink;
  359.         }
  360.         
  361.         if ( *driveQElementPtr == NULL )
  362.         {
  363.             /* This should never happen since every volume must have a drive, but... */
  364.             result = nsDrvErr;
  365.         }
  366.     }
  367.     
  368.     return ( result );
  369. }
  370.  
  371. /*****************************************************************************/
  372.  
  373. pascal    OSErr    UnmountAndEject(StringPtr pathname,
  374.                                 short vRefNum)
  375. {
  376.     HParamBlockRec pb;
  377.     Str255 tempPathname;
  378.     short driveNum;
  379.     Boolean ejected, wantsEject;
  380.     DrvQElPtr drvQElem;
  381.     OSErr error;
  382.  
  383.     pb.volumeParam.ioVRefNum = vRefNum;
  384.     if ( pathname == NULL )
  385.     {
  386.         pb.volumeParam.ioNamePtr = NULL;
  387.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  388.     }
  389.     else
  390.     {
  391.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  392.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  393.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  394.     }
  395.     error = PBHGetVInfoSync(&pb);
  396.     if ( error == noErr )
  397.     {
  398.         if ( pb.volumeParam.ioVDrvInfo != 0 )
  399.         {
  400.             /* the volume is online and not ejected */
  401.             ejected = false;
  402.             
  403.             /* Get the drive number */
  404.             driveNum = pb.volumeParam.ioVDrvInfo;
  405.         }
  406.         else
  407.         {
  408.             /* the volume is ejected or offline */
  409.             
  410.             /* Is it ejected? */
  411.             ejected = pb.volumeParam.ioVDRefNum > 0;
  412.             
  413.             if ( ejected )
  414.             {
  415.                 /* If ejected, the drive number is ioVDRefNum */
  416.                 driveNum = pb.volumeParam.ioVDRefNum;
  417.             }
  418.             else
  419.             {
  420.                 /* If offline, the drive number is the negative of ioVDRefNum */
  421.                 driveNum = (short)-pb.volumeParam.ioVDRefNum;
  422.             }
  423.         }
  424.         
  425.         /* find the drive queue element */
  426.         drvQElem = (DrvQElPtr)(GetDrvQHdr()->qHead);
  427.         while ( (drvQElem != NULL) && (drvQElem->dQDrive != driveNum) )
  428.         {
  429.             drvQElem = (DrvQElPtr)drvQElem->qLink;
  430.         }
  431.         
  432.         if ( drvQElem != NULL )
  433.         {
  434.             /* does the drive want an eject call */
  435.             wantsEject = (*((Ptr)((Ptr)drvQElem - 3)) != 8);
  436.         }
  437.         else
  438.         {
  439.             /* didn't find the drive!! */
  440.             wantsEject = false;
  441.         }
  442.         
  443.         /* unmount the volume */
  444.         pb.volumeParam.ioNamePtr = NULL;
  445.         /* ioVRefNum is already filled in from PBHGetVInfo */
  446.         error = PBUnmountVol((ParmBlkPtr)&pb);
  447.         if ( error == noErr )
  448.         {
  449.             if ( wantsEject && !ejected )
  450.             {
  451.                 /* eject the media from the drive if needed */
  452.                 pb.volumeParam.ioVRefNum = driveNum;
  453.                 error = PBEject((ParmBlkPtr)&pb);
  454.             }
  455.         }
  456.     }
  457.     return ( error );
  458. }
  459.  
  460. /*****************************************************************************/
  461.  
  462. pascal    OSErr    OnLine(FSSpecPtr volumes,
  463.                        short reqVolCount,
  464.                        short *actVolCount,
  465.                        short *volIndex)
  466. {
  467.     HParamBlockRec pb;
  468.     OSErr error = noErr;
  469.     FSSpec *endVolArray = volumes + reqVolCount;
  470.  
  471.     *actVolCount = 0;
  472.     for ( ; (volumes < endVolArray) && (error == noErr); ++volumes )
  473.     {
  474.         pb.volumeParam.ioNamePtr = (StringPtr) & volumes->name;
  475.         pb.volumeParam.ioVolIndex = *volIndex;
  476.         error = PBHGetVInfoSync(&pb);
  477.         if ( error == noErr )
  478.         {
  479.             volumes->parID = fsRtParID;        /* the root directory's parent is 1 */
  480.             volumes->vRefNum = pb.volumeParam.ioVRefNum;
  481.             ++*volIndex;
  482.             ++*actVolCount;
  483.         }
  484.     }
  485.     return ( error );
  486. }
  487.  
  488. /*****************************************************************************/
  489.  
  490. pascal    OSErr GetDInfo(short vRefNum,
  491.                        long dirID,
  492.                        StringPtr name,
  493.                        DInfo *fndrInfo)
  494. {
  495.     CInfoPBRec pb;
  496.     OSErr error;
  497.     
  498.     if ( (name != NULL) && (name[0] == 0) )
  499.         pb.dirInfo.ioNamePtr = NULL;
  500.     else
  501.         pb.dirInfo.ioNamePtr = name;
  502.     pb.dirInfo.ioVRefNum = vRefNum;
  503.     pb.dirInfo.ioDrDirID = dirID;
  504.     pb.dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDrDirID */
  505.     error = PBGetCatInfoSync(&pb);
  506.     if ( error == noErr )
  507.     {
  508.         if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  509.             /* it's a directory, return the DInfo */
  510.             *fndrInfo = pb.dirInfo.ioDrUsrWds;
  511.         else
  512.             /* oops, a file was passed */
  513.             error = dirNFErr;
  514.     }
  515.     return ( error );
  516. }
  517.  
  518. /*****************************************************************************/
  519.  
  520. pascal    OSErr FSpGetDInfo(const FSSpec *spec,
  521.                           DInfo *fndrInfo)
  522. {
  523.     return ( GetDInfo(spec->vRefNum, spec->parID, (StringPtr)spec->name, fndrInfo) );
  524. }
  525.  
  526. /*****************************************************************************/
  527.  
  528. pascal    OSErr SetDInfo(short vRefNum,
  529.                        long dirID,
  530.                        StringPtr name,
  531.                        const DInfo *fndrInfo)
  532. {
  533.     CInfoPBRec pb;
  534.     OSErr error;
  535.  
  536.     if ( (name != NULL) && (name[0] == 0) )
  537.         pb.dirInfo.ioNamePtr = NULL;
  538.     else
  539.         pb.dirInfo.ioNamePtr = name;
  540.     pb.dirInfo.ioVRefNum = vRefNum;
  541.     pb.dirInfo.ioDrDirID = dirID;
  542.     pb.dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDrDirID */
  543.     error = PBGetCatInfoSync(&pb);
  544.     if ( error == noErr )
  545.     {
  546.         if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  547.         {
  548.             /* it's a directory, set the DInfo */
  549.             pb.dirInfo.ioDrUsrWds = *fndrInfo;
  550.             pb.dirInfo.ioDrDirID = dirID;
  551.             error = PBSetCatInfoSync(&pb);
  552.         }
  553.         else
  554.             /* oops, a file was passed */
  555.             error = dirNFErr;
  556.     }
  557.     return ( error );
  558. }
  559.  
  560. /*****************************************************************************/
  561.  
  562. pascal    OSErr FSpSetDInfo(const FSSpec *spec,
  563.                           const DInfo *fndrInfo)
  564. {
  565.     return ( SetDInfo(spec->vRefNum, spec->parID, (StringPtr)spec->name, fndrInfo) );
  566. }
  567.  
  568. /*****************************************************************************/
  569.  
  570. pascal    OSErr    GetDirID(short vRefNum,
  571.                          long dirID,
  572.                          StringPtr name,
  573.                          long *theDirID,
  574.                          Boolean *isDirectory)
  575. {
  576.     CInfoPBRec pb;
  577.     OSErr error;
  578.  
  579.     if ( (name != NULL) && (name[0] == 0) )
  580.         pb.hFileInfo.ioNamePtr = NULL;
  581.     else
  582.         pb.hFileInfo.ioNamePtr = name;
  583.     pb.hFileInfo.ioVRefNum = vRefNum;
  584.     pb.hFileInfo.ioDirID = dirID;
  585.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  586.     error = PBGetCatInfoSync(&pb);
  587.     *theDirID = pb.hFileInfo.ioDirID;
  588.     *isDirectory = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  589.     return ( error );
  590. }
  591.  
  592. /*****************************************************************************/
  593.  
  594. pascal    OSErr    DirIDFromFSSpec(const FSSpec *spec,
  595.                                 long *theDirID,
  596.                                 Boolean *isDirectory)
  597. {
  598.     return ( GetDirID(spec->vRefNum, spec->parID, (StringPtr)spec->name,
  599.              theDirID, isDirectory) );
  600. }
  601.  
  602. /*****************************************************************************/
  603.  
  604. pascal    OSErr    GetDirName(short vRefNum,
  605.                            long dirID,
  606.                            StringPtr name)
  607. {
  608.     CInfoPBRec pb;
  609.  
  610.     pb.hFileInfo.ioNamePtr = name;
  611.     pb.hFileInfo.ioVRefNum = vRefNum;
  612.     pb.hFileInfo.ioDirID = dirID;
  613.     pb.hFileInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  614.     return ( PBGetCatInfoSync(&pb) );
  615. }
  616.  
  617. /*****************************************************************************/
  618.  
  619. pascal    OSErr    GetParentID(short vRefNum,
  620.                             long dirID,
  621.                             StringPtr name,
  622.                             long *parID)
  623. {
  624.     CInfoPBRec pb;
  625.     OSErr error;
  626.     short realVRefNum;
  627.     
  628.     if ( (name != NULL) && (name[0] == 0) )
  629.         pb.hFileInfo.ioNamePtr = NULL;
  630.     else
  631.         pb.hFileInfo.ioNamePtr = name;
  632.     pb.hFileInfo.ioVRefNum = vRefNum;
  633.     pb.hFileInfo.ioDirID = dirID;
  634.     pb.hFileInfo.ioFDirIndex = 0;                /* use ioNamePtr and ioDirID */
  635.     error = PBGetCatInfoSync(&pb);
  636.     if ( error == noErr )
  637.     {
  638.         /*
  639.         **    There's a bug in HFS where the wrong parent dir ID can be
  640.         **    returned if multiple separators are used at the end of a
  641.         **    pathname. For example, if the pathname:
  642.         **        'volumeName:System Folder:Extensions::'
  643.         **    is passed, the directory ID of the Extensions folder is
  644.         **    returned in the ioFlParID field instead of fsRtDirID. Since
  645.         **    multiple separators at the end of a pathname always specifies
  646.         **    a directory, we only need to work-around cases where the
  647.         **    object is a directory and there are multiple separators at
  648.         **    the end of the name parameter.
  649.         */
  650.         if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  651.         {
  652.             /* Its a directory */
  653.             
  654.             /* is there a pathname? */
  655.             if ( name != NULL )    
  656.             {
  657.                 /* could it contain multiple separators? */
  658.                 if ( name[0] >= 2 )
  659.                 {
  660.                     /* does it contain multiple separators at the end? */
  661.                     if ( (name[name[0]] == ':') && (name[name[0] - 1] == ':') )
  662.                     {
  663.                         /* OK, then do the extra stuff to get the correct parID */
  664.                         
  665.                         /* Get the real vRefNum (this should not fail) */
  666.                         error = DetermineVRefNum(name, vRefNum, &realVRefNum);
  667.                         if ( error == noErr )
  668.                         {
  669.                             pb.dirInfo.ioNamePtr = NULL;    /* we don't need the parent's name */
  670.                             pb.dirInfo.ioVRefNum = realVRefNum;
  671.                             /* pb.dirInfo.ioDrDirID already contains the */
  672.                             /* dirID of the directory object */
  673.                             pb.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  674.                             error = PBGetCatInfoSync(&pb);
  675.                             /* now, pb.dirInfo.ioDrParID contains the correct parID */
  676.                         }
  677.                     }
  678.                 }
  679.             }
  680.         }
  681.         
  682.         /* if no errors, then pb.hFileInfo.ioFlParID (pb.dirInfo.ioDrParID) */
  683.         /* contains the parent ID */
  684.         *parID = pb.hFileInfo.ioFlParID;
  685.     }
  686.     return ( error );
  687. }
  688.  
  689. /*****************************************************************************/
  690.  
  691. pascal    OSErr    GetFilenameFromPathname(ConstStr255Param pathname,
  692.                                         Str255 filename)
  693. {
  694.     short    index;
  695.     short    nameEnd;
  696.  
  697.     /* default to no filename */
  698.     filename[0] = 0;
  699.  
  700.     /* check for no pathname */
  701.     if ( pathname == NULL )
  702.         return ( notAFileErr );
  703.     
  704.     /* get string length */
  705.     index = pathname[0];
  706.     
  707.     /* check for empty string */
  708.     if ( index == 0 )
  709.         return ( notAFileErr );
  710.     
  711.     /* skip over last trailing colon (if any) */
  712.     if ( pathname[index] == ':' )
  713.         --index;
  714.  
  715.     /* save the end of the string */
  716.     nameEnd = index;
  717.  
  718.     /* if pathname ends with multiple colons, then this pathname refers */
  719.     /* to a directory, not a file */
  720.     if ( pathname[index] == ':' )
  721.         return ( notAFileErr );
  722.         
  723.     
  724.     /* parse backwards until we find a colon or hit the beginning of the pathname */
  725.     while ( (index != 0) && (pathname[index] != ':') )
  726.     {
  727.         --index;
  728.     }
  729.     
  730.     /* if we parsed to the beginning of the pathname and the pathname ended */
  731.     /* with a colon, then pathname is a full pathname to a volume, not a file */
  732.     if ( (index == 0) && (pathname[pathname[0]] == ':') )
  733.         return ( notAFileErr );
  734.     
  735.     /* get the filename and return noErr */
  736.     filename[0] = (char)(nameEnd - index);
  737.     BlockMoveData(&pathname[index+1], &filename[1], nameEnd - index);
  738.     return ( noErr );
  739. }
  740.  
  741. /*****************************************************************************/
  742.  
  743. pascal    OSErr    GetObjectLocation(short vRefNum,
  744.                                   long dirID,
  745.                                   StringPtr pathname,
  746.                                   short *realVRefNum,
  747.                                   long *realParID,
  748.                                   Str255 realName,
  749.                                   Boolean *isDirectory)
  750. {
  751.     OSErr error;
  752.     UniversalFMPB pb;
  753.     Str255 tempPathname;
  754.     
  755.     /* clear results */
  756.     *realVRefNum = 0;
  757.     *realParID = 0;
  758.     realName[0] = 0;
  759.     
  760.     /*
  761.     **    Get the real vRefNum
  762.     */
  763.     pb.hPB.volumeParam.ioVRefNum = vRefNum;
  764.     if ( pathname == NULL )
  765.     {
  766.         pb.hPB.volumeParam.ioNamePtr = NULL;
  767.         pb.hPB.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only */
  768.     }
  769.     else
  770.     {
  771.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  772.         pb.hPB.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  773.         pb.hPB.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  774.     }
  775.     error = PBHGetVInfoSync(&pb.hPB);
  776.  
  777.     if ( error == noErr )
  778.     {
  779.         *realVRefNum = pb.hPB.volumeParam.ioVRefNum;    /* we have the real vRefNum */
  780.         
  781.         /*
  782.         **    Determine if the object already exists and if so,
  783.         **    get the real parent directory ID if it's a file
  784.         */
  785.         if ( (pathname != NULL) && (pathname[0] == 0) )
  786.             pb.ciPB.hFileInfo.ioNamePtr = NULL;
  787.         else
  788.             pb.ciPB.hFileInfo.ioNamePtr = (StringPtr)pathname;
  789.         pb.ciPB.hFileInfo.ioVRefNum = vRefNum;
  790.         pb.ciPB.hFileInfo.ioDirID = dirID;
  791.         pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  792.         error = PBGetCatInfoSync(&pb.ciPB);
  793.         
  794.         if ( error == noErr )
  795.         {
  796.             /*
  797.             **    The file system object is present and we have the file's real parID
  798.             */
  799.             
  800.             /*    Is it a directory or a file? */
  801.             *isDirectory = (pb.ciPB.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  802.             if ( *isDirectory )
  803.             {
  804.                 /*
  805.                 **    It's a directory, get its name and parent dirID, and then we're done
  806.                 */
  807.                 
  808.                 pb.ciPB.dirInfo.ioNamePtr = realName;
  809.                 pb.ciPB.dirInfo.ioVRefNum = *realVRefNum;
  810.                 /* pb.ciPB.dirInfo.ioDrDirID already contains the dirID of the directory object */
  811.                 pb.ciPB.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  812.                 error = PBGetCatInfoSync(&pb.ciPB);
  813.                 
  814.                 /* get the parent ID here, because the file system can return the */
  815.                 /* wrong parent ID from the last call. */
  816.                 *realParID = pb.ciPB.dirInfo.ioDrParID;
  817.             }
  818.             else
  819.             {
  820.                 /*
  821.                 **    It's a file - use the parent directory ID from the last call
  822.                 **    to GetCatInfoparse, get the file name, and then we're done
  823.                 */
  824.                 *realParID = pb.ciPB.hFileInfo.ioFlParID;    
  825.                 error = GetFilenameFromPathname(pathname, realName);
  826.             }
  827.         }
  828.         else if ( error == fnfErr )
  829.         {
  830.             /*
  831.             **    The file system object is not present - see if its parent is present
  832.             */
  833.             
  834.             /*
  835.             **    Parse to get the object name from end of pathname
  836.             */
  837.             error = GetFilenameFromPathname(pathname, realName);
  838.             
  839.             /* if we can't get the object name from the end, we can't continue */
  840.             if ( error == noErr )
  841.             {
  842.                 /*
  843.                 **    What we want now is the pathname minus the object name
  844.                 **    for example:
  845.                 **    if pathname is 'vol:dir:file' tempPathname becomes 'vol:dir:'
  846.                 **    if pathname is 'vol:dir:file:' tempPathname becomes 'vol:dir:'
  847.                 **    if pathname is ':dir:file' tempPathname becomes ':dir:'
  848.                 **    if pathname is ':dir:file:' tempPathname becomes ':dir:'
  849.                 **    if pathname is ':file' tempPathname becomes ':'
  850.                 **    if pathname is 'file or file:' tempPathname becomes ''
  851.                 */
  852.                 
  853.                 /* get a copy of the pathname */
  854.                 BlockMoveData(pathname, tempPathname, pathname[0] + 1);
  855.                 
  856.                 /* remove the object name */
  857.                 tempPathname[0] -= realName[0];
  858.                 /* and the trailing colon (if any) */
  859.                 if ( pathname[pathname[0]] == ':' )
  860.                     --tempPathname[0];
  861.                 
  862.                 /* OK, now get the parent's directory ID */
  863.                 if ( tempPathname[0] != 0 )
  864.                     pb.ciPB.hFileInfo.ioNamePtr = (StringPtr)tempPathname;
  865.                 else
  866.                     pb.ciPB.hFileInfo.ioNamePtr = NULL;
  867.                 pb.ciPB.hFileInfo.ioVRefNum = vRefNum;
  868.                 pb.ciPB.hFileInfo.ioDirID = dirID;
  869.                 pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  870.                 error = PBGetCatInfoSync(&pb.ciPB);
  871.                 *realParID = pb.ciPB.dirInfo.ioDrDirID;
  872.  
  873.                 *isDirectory = false;    /* we don't know what the object is really going to be */
  874.             }
  875.             
  876.             if ( error != noErr )
  877.                 error = dirNFErr;    /* couldn't find parent directory */
  878.             else
  879.                 error = fnfErr;    /* we found the parent, but not the file */
  880.         }
  881.     }
  882.     return ( error );
  883. }
  884.  
  885. /*****************************************************************************/
  886.  
  887. pascal    OSErr    GetDirItems(short vRefNum,
  888.                             long dirID,
  889.                             StringPtr name,
  890.                             Boolean getFiles,
  891.                             Boolean getDirectories,
  892.                             FSSpecPtr items,
  893.                             short reqItemCount,
  894.                             short *actItemCount,
  895.                             short *itemIndex) /* start with 1, then use what's returned */
  896. {
  897.     CInfoPBRec pb;
  898.     OSErr error = noErr;
  899.     long theDirID;
  900.     Boolean isDirectory;
  901.     FSSpec *endItemsArray = items + reqItemCount;
  902.  
  903.     /* NOTE: If I could be sure that the caller passed a real vRefNum and real directory */
  904.     /* to this routine, I could rip out calls to DetermineVRefNum and GetDirID and this */
  905.     /* routine would be much faster because of the overhead of DetermineVRefNum and */
  906.     /* GetDirID and because GetDirID blows away the directory index hint the Macintosh */
  907.     /* file system keeps for indexed calls. I can't be sure, so for maximum throughput, */
  908.     /* pass a big array of FSSpecs so you can get the directory's contents with few calls */
  909.     /* to this routine. */
  910.     
  911.     /* get the real volume reference number */
  912.     error = DetermineVRefNum(name, vRefNum, &pb.hFileInfo.ioVRefNum);
  913.     if ( error != noErr )
  914.         return ( error );
  915.     
  916.     /* and the real directory ID of this directory (and make sure it IS a directory) */
  917.     error = GetDirID(vRefNum, dirID, name, &theDirID, &isDirectory);
  918.     if ( error != noErr )
  919.         return ( error );
  920.     else if ( !isDirectory )
  921.         return ( dirNFErr );
  922.  
  923.  
  924.     *actItemCount = 0;
  925.     for ( ; (items < endItemsArray) && (error == noErr); )
  926.     {
  927.         pb.hFileInfo.ioNamePtr = (StringPtr) &items->name;
  928.         pb.hFileInfo.ioDirID = theDirID;
  929.         pb.hFileInfo.ioFDirIndex = *itemIndex;
  930.         error = PBGetCatInfoSync(&pb);
  931.         if ( error == noErr )
  932.         {
  933.             items->parID = pb.hFileInfo.ioFlParID;    /* return item's parID */
  934.             items->vRefNum = pb.hFileInfo.ioVRefNum;    /* return item's vRefNum */
  935.             ++*itemIndex;    /* prepare to get next item in directory */
  936.             
  937.             if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  938.             {
  939.                 if ( getDirectories )
  940.                 {
  941.                     ++*actItemCount; /* keep this item */
  942.                     ++items; /* point to next item */
  943.                 }
  944.             }
  945.             else
  946.             {
  947.                 if ( getFiles )
  948.                 {
  949.                     ++*actItemCount; /* keep this item */
  950.                     ++items; /* point to next item */
  951.                 }
  952.             }
  953.         }
  954.     }
  955.     return ( error );
  956. }
  957.  
  958. /*****************************************************************************/
  959.  
  960. static    void    DeleteLevel(long dirToDelete,
  961.                             DeleteEnumGlobalsPtr theGlobals)
  962. {
  963.     long savedDir;
  964.     
  965.     do
  966.     {
  967.         /* prepare to delete directory */
  968.         theGlobals->myPB.ciPB.dirInfo.ioNamePtr = (StringPtr)&theGlobals->itemName;
  969.         theGlobals->myPB.ciPB.dirInfo.ioFDirIndex = 1;    /* get first item */
  970.         theGlobals->myPB.ciPB.dirInfo.ioDrDirID = dirToDelete;    /* in this directory */
  971.         theGlobals->error = PBGetCatInfoSync(&(theGlobals->myPB.ciPB));
  972.         if ( theGlobals->error == noErr )
  973.         {
  974.             savedDir = dirToDelete;
  975.             /* We have an item.  Is it a file or directory? */
  976.             if ( (theGlobals->myPB.ciPB.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  977.             {
  978.                 /* it's a directory */
  979.                 savedDir = theGlobals->myPB.ciPB.dirInfo.ioDrDirID;    /* save dirID of directory instead */
  980.                 DeleteLevel(theGlobals->myPB.ciPB.dirInfo.ioDrDirID, theGlobals);    /* Delete its contents */
  981.                 theGlobals->myPB.ciPB.dirInfo.ioNamePtr = NULL;    /* prepare to delete directory */
  982.             }
  983.             if ( theGlobals->error == noErr )
  984.             {
  985.                 theGlobals->myPB.ciPB.dirInfo.ioDrDirID = savedDir;    /* restore dirID */
  986.                 theGlobals->myPB.hPB.fileParam.ioFVersNum = 0;    /* just in case it's used on an MFS volume... */
  987.                 theGlobals->error = PBHDeleteSync(&(theGlobals->myPB.hPB));    /* delete this item */
  988.                 if ( theGlobals->error == fLckdErr )
  989.                 {
  990.                     (void) PBHRstFLockSync(&(theGlobals->myPB.hPB));    /* unlock it */
  991.                     theGlobals->error = PBHDeleteSync(&(theGlobals->myPB.hPB));    /* and try again */
  992.                 }
  993.             }
  994.         }
  995.     } while ( theGlobals->error == noErr );
  996.     
  997.     if ( theGlobals->error == fnfErr )
  998.         theGlobals->error = noErr;
  999. }
  1000.  
  1001. /*****************************************************************************/
  1002.  
  1003. pascal    OSErr    DeleteDirectoryContents(short vRefNum,
  1004.                                          long dirID,
  1005.                                         StringPtr name)
  1006. {
  1007.     DeleteEnumGlobals theGlobals;
  1008.     Boolean    isDirectory;
  1009.     OSErr error;
  1010.  
  1011.     /*  Get the real dirID and make sure it is a directory. */
  1012.     error = GetDirID(vRefNum, dirID, name, &dirID, &isDirectory);
  1013.     if ( error != noErr )
  1014.         return ( error );
  1015.     if ( !isDirectory )
  1016.         return ( dirNFErr );
  1017.     
  1018.     /* Get the real vRefNum */
  1019.     error = DetermineVRefNum(name, vRefNum, &vRefNum);
  1020.     if ( error != noErr )
  1021.         return ( error );
  1022.     
  1023.     /* Set up the globals we need to access from the recursive routine. */
  1024.     theGlobals.myPB.ciPB.dirInfo.ioVRefNum = vRefNum;
  1025.         
  1026.     /* Here we go into recursion land... */
  1027.     DeleteLevel(dirID, &theGlobals);
  1028.     return ( theGlobals.error );
  1029. }
  1030.  
  1031. /*****************************************************************************/
  1032.  
  1033. pascal    OSErr    DeleteDirectory(short vRefNum,
  1034.                                 long dirID,
  1035.                                 StringPtr name)
  1036. {
  1037.     OSErr error;
  1038.     
  1039.     /* Make sure a directory was specified and then delete its contents */
  1040.     error = DeleteDirectoryContents(vRefNum, dirID, name);
  1041.     if ( error == noErr )
  1042.     {
  1043.         error = HDelete(vRefNum, dirID, name);
  1044.         if ( error == fLckdErr )
  1045.         {
  1046.             (void) HRstFLock(vRefNum, dirID, name);    /* unlock the directory locked by AppleShare */
  1047.             error = HDelete(vRefNum, dirID, name);    /* and try again */
  1048.         }
  1049.     }
  1050.     return ( error );
  1051. }
  1052.  
  1053. /*****************************************************************************/
  1054.  
  1055. pascal    OSErr    CheckObjectLock(short vRefNum,
  1056.                                 long dirID,
  1057.                                 StringPtr name)
  1058. {
  1059.     CInfoPBRec pb;
  1060.     OSErr error;
  1061.     
  1062.     if ( (name != NULL) && (name[0] == 0) )
  1063.         pb.hFileInfo.ioNamePtr = NULL;
  1064.     else
  1065.         pb.hFileInfo.ioNamePtr = name;
  1066.     pb.hFileInfo.ioVRefNum = vRefNum;
  1067.     pb.hFileInfo.ioDirID = dirID;
  1068.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1069.     error = PBGetCatInfoSync(&pb);
  1070.     
  1071.     if ( error == noErr )
  1072.     {
  1073.         /* check locked bit */
  1074.         if ( (pb.hFileInfo.ioFlAttrib & 0x01) != 0 )
  1075.             error = fLckdErr;
  1076.     }
  1077.     return ( error );
  1078. }
  1079.  
  1080. /*****************************************************************************/
  1081.  
  1082. pascal    OSErr    FSpCheckObjectLock(const FSSpec *spec)
  1083. {
  1084.     return ( CheckObjectLock(spec->vRefNum, spec->parID, (StringPtr)spec->name) );
  1085. }
  1086.  
  1087. /*****************************************************************************/
  1088.  
  1089. pascal    OSErr    GetFileSize(short vRefNum,
  1090.                             long dirID,
  1091.                             ConstStr255Param fileName,
  1092.                             long *dataSize,
  1093.                             long *rsrcSize)
  1094. {
  1095.     HParamBlockRec pb;
  1096.     OSErr error;
  1097.     
  1098.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1099.     pb.fileParam.ioVRefNum = vRefNum;
  1100.     pb.fileParam.ioFVersNum = 0;
  1101.     pb.fileParam.ioDirID = dirID;
  1102.     pb.fileParam.ioFDirIndex = 0;
  1103.     error = PBHGetFInfoSync(&pb);
  1104.     if ( error == noErr )
  1105.     {
  1106.         *dataSize = pb.fileParam.ioFlLgLen;
  1107.         *rsrcSize = pb.fileParam.ioFlRLgLen;
  1108.     }
  1109.     return ( error );
  1110. }
  1111.  
  1112. /*****************************************************************************/
  1113.  
  1114. pascal    OSErr    FSpGetFileSize(const FSSpec *spec,
  1115.                                long *dataSize,
  1116.                                long *rsrcSize)
  1117. {
  1118.     return ( GetFileSize(spec->vRefNum, spec->parID, spec->name, dataSize, rsrcSize) );
  1119. }
  1120.  
  1121. /*****************************************************************************/
  1122.  
  1123. pascal    OSErr    BumpDate(short vRefNum,
  1124.                          long dirID,
  1125.                          StringPtr name)
  1126. /* Given a file or directory, change its modification date to the current date/time. */
  1127. {
  1128.     CInfoPBRec pb;
  1129.     OSErr error;
  1130.     unsigned long secs;
  1131.  
  1132.     if ( (name != NULL) && (name[0] == 0) )
  1133.         pb.hFileInfo.ioNamePtr = NULL;
  1134.     else
  1135.         pb.hFileInfo.ioNamePtr = name;
  1136.     pb.hFileInfo.ioVRefNum = vRefNum;
  1137.     pb.hFileInfo.ioDirID = dirID;
  1138.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1139.     error = PBGetCatInfoSync(&pb);
  1140.     if ( error == noErr )
  1141.     {
  1142.         GetDateTime(&secs);
  1143.         /* set mod date to current date, or one second into the future
  1144.             if mod date = current date */
  1145.         pb.hFileInfo.ioFlMdDat = (secs == pb.hFileInfo.ioFlMdDat) ? (++secs) : (secs);
  1146.         pb.hFileInfo.ioDirID = dirID;
  1147.         error = PBSetCatInfoSync(&pb);
  1148.     }
  1149.     return ( error );
  1150. }
  1151.  
  1152. /*****************************************************************************/
  1153.  
  1154. pascal    OSErr    FSpBumpDate(const FSSpec *spec)
  1155. {
  1156.     return ( BumpDate(spec->vRefNum, spec->parID, (StringPtr)spec->name) );
  1157. }
  1158.  
  1159. /*****************************************************************************/
  1160.  
  1161. pascal    OSErr    ChangeCreatorType(short vRefNum,
  1162.                                   long dirID,
  1163.                                   ConstStr255Param name,
  1164.                                   OSType creator,
  1165.                                   OSType fileType)
  1166. {
  1167.     CInfoPBRec pb;
  1168.     OSErr error;
  1169.     short realVRefNum;
  1170.     long parID;
  1171.  
  1172.     pb.hFileInfo.ioNamePtr = (StringPtr)name;
  1173.     pb.hFileInfo.ioVRefNum = vRefNum;
  1174.     pb.hFileInfo.ioDirID = dirID;
  1175.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1176.     error = PBGetCatInfoSync(&pb);
  1177.     if ( error == noErr )
  1178.     {
  1179.         if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )    /* if directory */
  1180.             return ( notAFileErr );            /* do nothing and return error */
  1181.             
  1182.         parID = pb.hFileInfo.ioFlParID;    /* save parent dirID for BumpDate call */
  1183.  
  1184.         /* If creator not 0x00000000, change creator */
  1185.         if ( creator != (OSType)0x00000000 )
  1186.             pb.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  1187.         
  1188.         /* If fileType not 0x00000000, change fileType */
  1189.         if ( fileType != (OSType)0x00000000 )
  1190.             pb.hFileInfo.ioFlFndrInfo.fdType = fileType;
  1191.             
  1192.         pb.hFileInfo.ioDirID = dirID;
  1193.         error = PBSetCatInfoSync(&pb);    /* now, save the new information back to disk */
  1194.  
  1195.         if ( (error == noErr) && (parID != fsRtParID) ) /* can't bump fsRtParID */
  1196.         {
  1197.             /* get the real vRefNum in case a full pathname was passed */
  1198.             error = DetermineVRefNum((StringPtr)name, vRefNum, &realVRefNum);
  1199.             if ( error == noErr )
  1200.             {
  1201.                 error = BumpDate(realVRefNum, parID, NULL);
  1202.                     /* and bump the parent directory's mod date to wake up the Finder */
  1203.                     /* to the change we just made */
  1204.             }
  1205.         }
  1206.     }
  1207.     return ( error );
  1208. }
  1209.  
  1210. /*****************************************************************************/
  1211.  
  1212. pascal    OSErr    FSpChangeCreatorType(const FSSpec *spec,
  1213.                                      OSType creator,
  1214.                                      OSType fileType)
  1215. {
  1216.     return ( ChangeCreatorType(spec->vRefNum, spec->parID, spec->name, creator, fileType) );
  1217. }
  1218.  
  1219. /*****************************************************************************/
  1220.  
  1221. pascal    OSErr    ChangeFDFlags(short vRefNum,
  1222.                               long dirID,
  1223.                               StringPtr name,
  1224.                               Boolean    setBits,
  1225.                               unsigned short flagBits)
  1226. {
  1227.     CInfoPBRec pb;
  1228.     OSErr error;
  1229.     short realVRefNum;
  1230.     long parID;
  1231.  
  1232.     if ( (name != NULL) && (name[0] == 0) )
  1233.         pb.hFileInfo.ioNamePtr = NULL;
  1234.     else
  1235.         pb.hFileInfo.ioNamePtr = name;
  1236.     pb.hFileInfo.ioVRefNum = vRefNum;
  1237.     pb.hFileInfo.ioDirID = dirID;
  1238.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1239.     error = PBGetCatInfoSync(&pb);
  1240.     if ( error == noErr )
  1241.     {
  1242.         parID = pb.hFileInfo.ioFlParID;    /* save parent dirID for BumpDate call */
  1243.  
  1244.         pb.hFileInfo.ioFlFndrInfo.fdFlags = ( setBits ) ? 
  1245.             ( pb.hFileInfo.ioFlFndrInfo.fdFlags | flagBits ) : /* OR in the bits */
  1246.             ( pb.hFileInfo.ioFlFndrInfo.fdFlags & (0xffff ^ flagBits) ); /* AND out the bits */
  1247.                 /* set or clear the appropriate bits in the Finder flags */
  1248.             
  1249.         pb.hFileInfo.ioDirID = dirID;
  1250.         error = PBSetCatInfoSync(&pb);    /* now, save the new information back to disk */
  1251.  
  1252.         if ( (error == noErr) && (parID != fsRtParID) ) /* can't bump fsRtParID */
  1253.         {
  1254.             /* get the real vRefNum in case a full pathname was passed */
  1255.             error = DetermineVRefNum((StringPtr)name, vRefNum, &realVRefNum);
  1256.             if ( error == noErr )
  1257.             {
  1258.                 error = BumpDate(realVRefNum, parID, NULL);
  1259.                     /* and bump the parent directory's mod date to wake up the Finder */
  1260.                     /* to the change we just made */
  1261.             }
  1262.         }
  1263.     }
  1264.     return ( error );
  1265. }
  1266.  
  1267. /*****************************************************************************/
  1268.  
  1269. pascal    OSErr    FSpChangeFDFlags(const FSSpec *spec,
  1270.                                  Boolean setBits,
  1271.                                  unsigned short flagBits)
  1272. {
  1273.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, setBits, flagBits) );
  1274. }
  1275.  
  1276. /*****************************************************************************/
  1277.  
  1278. pascal    OSErr    SetIsInvisible(short vRefNum,
  1279.                                long dirID,
  1280.                                StringPtr name)
  1281.     /* Given a file or directory, make it invisible. */
  1282. {
  1283.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x4000) );
  1284. }
  1285.  
  1286. /*****************************************************************************/
  1287.  
  1288. pascal    OSErr    FSpSetIsInvisible(const FSSpec *spec)
  1289.     /* Given a file or directory, make it invisible. */
  1290. {
  1291.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x4000) );
  1292. }
  1293.  
  1294. /*****************************************************************************/
  1295.  
  1296. pascal    OSErr    ClearIsInvisible(short vRefNum,
  1297.                                  long dirID,
  1298.                                  StringPtr name)
  1299.     /* Given a file or directory, make it visible. */
  1300. {
  1301.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x4000) );
  1302. }
  1303.  
  1304. /*****************************************************************************/
  1305.  
  1306. pascal    OSErr    FSpClearIsInvisible(const FSSpec *spec)
  1307.     /* Given a file or directory, make it visible. */
  1308. {
  1309.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x4000) );
  1310. }
  1311.  
  1312. /*****************************************************************************/
  1313.  
  1314. pascal    OSErr    SetNameLocked(short vRefNum,
  1315.                               long dirID,
  1316.                               StringPtr name)
  1317.     /* Given a file or directory, lock its name. */
  1318. {
  1319.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x1000) );
  1320. }
  1321.  
  1322. /*****************************************************************************/
  1323.  
  1324. pascal    OSErr    FSpSetNameLocked(const FSSpec *spec)
  1325.     /* Given a file or directory, lock its name. */
  1326. {
  1327.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x1000) );
  1328. }
  1329.  
  1330. /*****************************************************************************/
  1331.  
  1332. pascal    OSErr    ClearNameLocked(short vRefNum,
  1333.                                 long dirID,
  1334.                                 StringPtr name)
  1335.     /* Given a file or directory, unlock its name. */
  1336. {
  1337.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x1000) );
  1338. }
  1339.  
  1340. /*****************************************************************************/
  1341.  
  1342. pascal    OSErr    FSpClearNameLocked(const FSSpec *spec)
  1343.     /* Given a file or directory, unlock its name. */
  1344. {
  1345.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x1000) );
  1346. }
  1347.  
  1348. /*****************************************************************************/
  1349.  
  1350. pascal    OSErr    SetIsStationery(short vRefNum,
  1351.                                 long dirID,
  1352.                                 ConstStr255Param name)
  1353.     /* Given a file, make it a stationery pad. */
  1354. {
  1355.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, true, 0x0800) );
  1356. }
  1357.  
  1358. /*****************************************************************************/
  1359.  
  1360. pascal    OSErr    FSpSetIsStationery(const FSSpec *spec)
  1361.     /* Given a file, make it a stationery pad. */
  1362. {
  1363.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x0800) );
  1364. }
  1365.  
  1366. /*****************************************************************************/
  1367.  
  1368. pascal    OSErr    ClearIsStationery(short vRefNum,
  1369.                                   long dirID,
  1370.                                   ConstStr255Param name)
  1371.     /* Given a file, clear the stationery bit. */
  1372. {
  1373.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, false, 0x0800) );
  1374. }
  1375.  
  1376. /*****************************************************************************/
  1377.  
  1378. pascal    OSErr    FSpClearIsStationery(const FSSpec *spec)
  1379.     /* Given a file, clear the stationery bit. */
  1380. {
  1381.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0800) );
  1382. }
  1383.  
  1384. /*****************************************************************************/
  1385.  
  1386. pascal    OSErr    SetHasCustomIcon(short vRefNum,
  1387.                                  long dirID,
  1388.                                  StringPtr name)
  1389.     /* Given a file or directory, indicate that it has a custom icon. */
  1390. {
  1391.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x0400) );
  1392. }
  1393.  
  1394. /*****************************************************************************/
  1395.  
  1396. pascal    OSErr    FSpSetHasCustomIcon(const FSSpec *spec)
  1397.     /* Given a file or directory, indicate that it has a custom icon. */
  1398. {
  1399.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x0400) );
  1400. }
  1401.  
  1402. /*****************************************************************************/
  1403.  
  1404. pascal    OSErr    ClearHasCustomIcon(short vRefNum,
  1405.                                    long dirID,
  1406.                                    StringPtr name)
  1407.     /* Given a file or directory, indicate that it does not have a custom icon. */
  1408. {
  1409.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x0400) );
  1410. }
  1411.  
  1412. /*****************************************************************************/
  1413.  
  1414. pascal    OSErr    FSpClearHasCustomIcon(const FSSpec *spec)
  1415.     /* Given a file or directory, indicate that it does not have a custom icon. */
  1416. {
  1417.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0400) );
  1418. }
  1419.  
  1420. /*****************************************************************************/
  1421.  
  1422. pascal    OSErr    ClearHasBeenInited(short vRefNum,
  1423.                                    long dirID,
  1424.                                    StringPtr name)
  1425.     /* Given a file, clear its "has been inited" bit. */
  1426. {
  1427.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, false, 0x0100) );
  1428. }
  1429.  
  1430. /*****************************************************************************/
  1431.  
  1432. pascal    OSErr    FSpClearHasBeenInited(const FSSpec *spec)
  1433.     /* Given a file, clear its "has been inited" bit. */
  1434. {
  1435.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0100) );
  1436. }
  1437.  
  1438. /*****************************************************************************/
  1439.  
  1440. pascal    OSErr    CopyFileMgrAttributes(short srcVRefNum,
  1441.                                       long srcDirID,
  1442.                                       StringPtr srcName,
  1443.                                       short dstVRefNum,
  1444.                                       long dstDirID,
  1445.                                       StringPtr dstName,
  1446.                                       Boolean copyLockBit)
  1447. {
  1448.     UniversalFMPB pb;
  1449.     OSErr error;
  1450.     Boolean objectIsDirectory;
  1451.  
  1452.     pb.ciPB.hFileInfo.ioVRefNum = srcVRefNum;
  1453.     pb.ciPB.hFileInfo.ioDirID = srcDirID;
  1454.     if ( (srcName != NULL) && (srcName[0] == 0) )
  1455.         pb.ciPB.hFileInfo.ioNamePtr = NULL;
  1456.     else
  1457.         pb.ciPB.hFileInfo.ioNamePtr = srcName;
  1458.     pb.ciPB.hFileInfo.ioFDirIndex = 0;
  1459.     error = PBGetCatInfoSync(&pb.ciPB);
  1460.     if ( error == noErr )
  1461.     {
  1462.         objectIsDirectory = ( (pb.ciPB.hFileInfo.ioFlAttrib & ioDirMask) != 0 );
  1463.         pb.ciPB.hFileInfo.ioVRefNum = dstVRefNum;
  1464.         pb.ciPB.hFileInfo.ioDirID = dstDirID;
  1465.         if ( (dstName != NULL) && (dstName[0] == 0) )
  1466.             pb.ciPB.hFileInfo.ioNamePtr = NULL;
  1467.         else
  1468.             pb.ciPB.hFileInfo.ioNamePtr = dstName;
  1469.         /* don't copy the hasBeenInited bit */
  1470.         pb.ciPB.hFileInfo.ioFlFndrInfo.fdFlags = ( pb.ciPB.hFileInfo.ioFlFndrInfo.fdFlags & 0xfeff );
  1471.         error = PBSetCatInfoSync(&pb.ciPB);
  1472.         if ( (error == noErr) && (copyLockBit) && ((pb.ciPB.hFileInfo.ioFlAttrib & 0x01) != 0) )
  1473.         {
  1474.             pb.hPB.fileParam.ioFVersNum = 0;
  1475.             error = PBHSetFLockSync(&pb.hPB);
  1476.             if ( (error != noErr) && (objectIsDirectory) )
  1477.                 error = noErr; /* ignore lock errors if destination is directory */
  1478.         }
  1479.     }
  1480.     return ( error );
  1481. }
  1482.  
  1483. /*****************************************************************************/
  1484.  
  1485. pascal    OSErr    FSpCopyFileMgrAttributes(const FSSpec *srcSpec,
  1486.                                          const FSSpec *dstSpec,
  1487.                                          Boolean copyLockBit)
  1488. {
  1489.     return ( CopyFileMgrAttributes(srcSpec->vRefNum, srcSpec->parID, (StringPtr)srcSpec->name,
  1490.                                    dstSpec->vRefNum, dstSpec->parID, (StringPtr)dstSpec->name,
  1491.                                    copyLockBit) );
  1492. }
  1493.  
  1494. /*****************************************************************************/
  1495.  
  1496. pascal    OSErr    HOpenAware(short vRefNum,
  1497.                            long dirID,
  1498.                            ConstStr255Param fileName,
  1499.                            short denyModes,
  1500.                            short *refNum)
  1501. {
  1502.     HParamBlockRec pb;
  1503.     OSErr error;
  1504.     GetVolParmsInfoBuffer volParmsInfo;
  1505.     long infoSize = sizeof(GetVolParmsInfoBuffer);
  1506.  
  1507.     pb.ioParam.ioMisc = NULL;
  1508.     pb.fileParam.ioFVersNum = 0;
  1509.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1510.     pb.fileParam.ioVRefNum = vRefNum;
  1511.     pb.fileParam.ioDirID = dirID;
  1512.  
  1513.     /* get volume attributes */
  1514.     /* this preflighting is needed because Foreign File Access based file systems don't */
  1515.     /* return the correct error result to the OpenDeny call */
  1516.     error = HGetVolParms((StringPtr)fileName, vRefNum, &volParmsInfo, &infoSize);
  1517.     if ( error == noErr )
  1518.     {
  1519.         /* if volume supports OpenDeny, use it and return */
  1520.         if ( hasOpenDeny(volParmsInfo) )
  1521.         {
  1522.             pb.accessParam.ioDenyModes = denyModes;
  1523.             error = PBHOpenDenySync(&pb);
  1524.             *refNum = pb.ioParam.ioRefNum;
  1525.             return ( error );
  1526.         }
  1527.     }
  1528.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1529.         return ( error );
  1530.     
  1531.     /* OpenDeny isn't supported, so try File Manager Open functions */
  1532.     
  1533.     /* If request includes write permission, then see if the volume is */
  1534.     /* locked by hardware or software. The HFS file system doesn't check */
  1535.     /* for this when a file is opened - you only find out later when you */
  1536.     /* try to write and the write fails with a wPrErr or a vLckdErr. */
  1537.     
  1538.     if ( (denyModes & dmWr) != 0 )
  1539.     {
  1540.         error = CheckVolLock((StringPtr)fileName, vRefNum);
  1541.         if ( error != noErr )
  1542.             return ( error );
  1543.     }
  1544.     
  1545.     /* Set File Manager permissions to closest thing possible */
  1546.     pb.ioParam.ioPermssn = ((denyModes == dmWr) || (denyModes == dmRdWr)) ?
  1547.                            (fsRdWrShPerm) :
  1548.                            (denyModes % 4);
  1549.  
  1550.     error = PBHOpenDFSync(&pb);                /* Try OpenDF */
  1551.     if ( error == paramErr )
  1552.         error = PBHOpenSync(&pb);            /* OpenDF not supported, so try Open */
  1553.     *refNum = pb.ioParam.ioRefNum;
  1554.     return ( error );
  1555. }
  1556.  
  1557. /*****************************************************************************/
  1558.  
  1559. pascal    OSErr    FSpOpenAware(const FSSpec *spec,
  1560.                              short denyModes,
  1561.                              short *refNum)
  1562. {
  1563.     return ( HOpenAware(spec->vRefNum, spec->parID, spec->name, denyModes, refNum) );
  1564. }
  1565.  
  1566. /*****************************************************************************/
  1567.  
  1568. pascal    OSErr    HOpenRFAware(short vRefNum,
  1569.                              long dirID,
  1570.                              ConstStr255Param fileName,
  1571.                              short denyModes,
  1572.                              short *refNum)
  1573. {
  1574.     HParamBlockRec pb;
  1575.     OSErr error;
  1576.     GetVolParmsInfoBuffer volParmsInfo;
  1577.     long infoSize = sizeof(GetVolParmsInfoBuffer);
  1578.  
  1579.     pb.ioParam.ioMisc = NULL;
  1580.     pb.fileParam.ioFVersNum = 0;
  1581.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1582.     pb.fileParam.ioVRefNum = vRefNum;
  1583.     pb.fileParam.ioDirID = dirID;
  1584.  
  1585.     /* get volume attributes */
  1586.     /* this preflighting is needed because Foreign File Access based file systems don't */
  1587.     /* return the correct error result to the OpenRFDeny call */
  1588.     error = HGetVolParms((StringPtr)fileName, vRefNum, &volParmsInfo, &infoSize);
  1589.     if ( error == noErr )
  1590.     {
  1591.         /* if volume supports OpenRFDeny, use it and return */
  1592.         if ( hasOpenDeny(volParmsInfo) )
  1593.         {
  1594.             pb.accessParam.ioDenyModes = denyModes;
  1595.             error = PBHOpenRFDenySync(&pb);
  1596.             *refNum = pb.ioParam.ioRefNum;
  1597.             return ( error );
  1598.         }
  1599.     }
  1600.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1601.         return ( error );
  1602.  
  1603.     /* OpenRFDeny isn't supported, so try File Manager OpenRF function */
  1604.     
  1605.     /* If request includes write permission, then see if the volume is */
  1606.     /* locked by hardware or software. The HFS file system doesn't check */
  1607.     /* for this when a file is opened - you only find out later when you */
  1608.     /* try to write and the write fails with a wPrErr or a vLckdErr. */
  1609.     
  1610.     if ( (denyModes & dmWr) != 0 )
  1611.     {
  1612.         error = CheckVolLock((StringPtr)fileName, vRefNum);
  1613.         if ( error != noErr )
  1614.             return ( error );
  1615.     }
  1616.     
  1617.     /* Set File Manager permissions to closest thing possible */
  1618.     pb.ioParam.ioPermssn = ((denyModes == dmWr) || (denyModes == dmRdWr)) ?
  1619.                            (fsRdWrShPerm) :
  1620.                            (denyModes % 4);
  1621.  
  1622.     error = PBHOpenRFSync(&pb);
  1623.     *refNum = pb.ioParam.ioRefNum;
  1624.     return ( error );
  1625. }
  1626.  
  1627. /*****************************************************************************/
  1628.  
  1629. pascal    OSErr    FSpOpenRFAware(const FSSpec *spec,
  1630.                                short denyModes,
  1631.                                short *refNum)
  1632. {
  1633.     return ( HOpenRFAware(spec->vRefNum, spec->parID, spec->name, denyModes, refNum) );
  1634. }
  1635.  
  1636. /*****************************************************************************/
  1637.  
  1638. pascal    OSErr    FSReadNoCache(short refNum,
  1639.                               long *count,
  1640.                               void *buffPtr)
  1641. {
  1642.     ParamBlockRec pb;
  1643.     OSErr error;
  1644.  
  1645.     pb.ioParam.ioRefNum = refNum;
  1646.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1647.     pb.ioParam.ioReqCount = *count;
  1648.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;    /* fsAtMark + noCacheBit */
  1649.     pb.ioParam.ioPosOffset = 0;
  1650.     error = PBReadSync(&pb);
  1651.     *count = pb.ioParam.ioActCount;
  1652.     return ( error );
  1653. }
  1654.  
  1655. /*****************************************************************************/
  1656.  
  1657. pascal    OSErr    FSWriteNoCache(short refNum,
  1658.                                long *count,
  1659.                                const void *buffPtr)
  1660. {
  1661.     ParamBlockRec pb;
  1662.     OSErr error;
  1663.  
  1664.     pb.ioParam.ioRefNum = refNum;
  1665.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1666.     pb.ioParam.ioReqCount = *count;
  1667.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;    /* fsAtMark + noCacheBit */
  1668.     pb.ioParam.ioPosOffset = 0;
  1669.     error = PBWriteSync(&pb);
  1670.     *count = pb.ioParam.ioActCount;
  1671.     return ( error );
  1672. }
  1673.  
  1674. /*****************************************************************************/
  1675.  
  1676. /*
  1677. **    See if numBytes bytes of buffer1 are equal to buffer2.
  1678. */
  1679. static    Boolean EqualMemory(const void *buffer1, const void *buffer2, unsigned long numBytes)
  1680. {
  1681.     register unsigned char *b1 = (unsigned char *)buffer1;
  1682.     register unsigned char *b2 = (unsigned char *)buffer2;
  1683.  
  1684.     if (b1 != b2)    /* if buffer pointers are same, then they are equal */
  1685.     {
  1686.         while ( numBytes > 0 )
  1687.         {
  1688.             /* compare the bytes and then increment the pointers */
  1689.             if ( (*b1++ - *b2++) != 0 )
  1690.             {
  1691.                 return ( false );
  1692.             }
  1693.             --numBytes;
  1694.         }
  1695.     }
  1696.     return ( true );
  1697. }
  1698.  
  1699. /*****************************************************************************/
  1700.  
  1701. /*
  1702. **    Read any number of bytes from an open file using read-verify mode.
  1703. **    The FSReadVerify function reads any number of bytes from an open file
  1704. **    and verifies them against the data in the buffer pointed to by buffPtr.
  1705. **    
  1706. **    Because of a bug in the HFS file system, only non-block aligned parts of
  1707. **    the read are verified against the buffer data and the rest is *copied*
  1708. **    into the buffer.  Thus, you shouldn't verify against your original data;
  1709. **    instead, you should verify against a copy of the original data and then
  1710. **    compare the read-verified copy against the original data after calling
  1711. **    FSReadVerify. That's why this function isn't exported - it needs the
  1712. **    wrapper provided by FSWriteVerify.
  1713. */
  1714. static    OSErr    FSReadVerify(short refNum,
  1715.                              long *count,
  1716.                              void *buffPtr)
  1717. {
  1718.     ParamBlockRec    pb;
  1719.     OSErr            result;
  1720.  
  1721.     pb.ioParam.ioRefNum = refNum;
  1722.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1723.     pb.ioParam.ioReqCount = *count;
  1724.     pb.ioParam.ioPosMode = fsAtMark + rdVerify;
  1725.     pb.ioParam.ioPosOffset = 0;
  1726.     result = PBReadSync(&pb);
  1727.     *count = pb.ioParam.ioActCount;
  1728.     return ( result );
  1729. }
  1730.  
  1731. /*****************************************************************************/
  1732.  
  1733. pascal    OSErr    FSWriteVerify(short refNum,
  1734.                               long *count,
  1735.                               const void *buffPtr)
  1736. {
  1737.     Ptr        verifyBuffer;
  1738.     long    position;
  1739.     long    bufferSize;
  1740.     long    byteCount;
  1741.     long    bytesVerified;
  1742.     Ptr        startVerify;
  1743.     OSErr    result;
  1744.     
  1745.     /*
  1746.     **    Allocate the verify buffer
  1747.     **    Try to get get a large enough buffer to verify in one pass.
  1748.     **    If that fails, use GetTempBuffer to get a buffer.
  1749.     */
  1750.     bufferSize = *count;
  1751.     verifyBuffer = NewPtr(bufferSize);
  1752.     if ( verifyBuffer == NULL )
  1753.     {
  1754.         verifyBuffer = GetTempBuffer(bufferSize, &bufferSize);
  1755.     }
  1756.     if ( verifyBuffer != NULL )
  1757.     {        
  1758.         /* Save the current position */
  1759.         result = GetFPos(refNum, &position);
  1760.         if ( result == noErr )
  1761.         {
  1762.             /* Write the data */
  1763.             result = FSWrite(refNum, count, buffPtr);
  1764.             if ( result == noErr )
  1765.             {
  1766.                 /* Restore the original position */
  1767.                 result = SetFPos(refNum, fsFromStart, position);
  1768.                 if ( result == noErr )
  1769.                 {
  1770.                     /*
  1771.                     **    *count            = total number of bytes to verify
  1772.                     **    bufferSize        = the size of the verify buffer
  1773.                     **    bytesVerified    = number of bytes verified
  1774.                     **    byteCount        = number of bytes to verify this pass
  1775.                     **    startVerify        = position in buffPtr
  1776.                     */
  1777.                     bytesVerified = 0;
  1778.                     startVerify = (Ptr)buffPtr;
  1779.                     while ( (bytesVerified < *count) && ( result == noErr ) )
  1780.                     {
  1781.                         byteCount = ((*count - bytesVerified) > bufferSize) ?
  1782.                                     (bufferSize) :
  1783.                                     (*count - bytesVerified);
  1784.                         /*
  1785.                         **    Copy the write buffer into the verify buffer.
  1786.                         **    This step is needed because the File Manager
  1787.                         **    compares the data in any non-block aligned
  1788.                         **    data at the beginning and end of the read-verify
  1789.                         **    request back into the file system's cache
  1790.                         **    to the data in verify Buffer. However, the
  1791.                         **    File Manager does not compare any full blocks
  1792.                         **    and instead copies them into the verify buffer
  1793.                         **    so we still have to compare the buffers again
  1794.                         **    after the read-verify request completes.
  1795.                         */
  1796.                         BlockMoveData(startVerify, verifyBuffer, byteCount);
  1797.                         
  1798.                         /* Read-verify the data back into the verify buffer */
  1799.                         result = FSReadVerify(refNum, &byteCount, verifyBuffer);
  1800.                         if ( result == noErr )
  1801.                         {
  1802.                             /* See if the buffers are the same */
  1803.                             if ( !EqualMemory(verifyBuffer, startVerify, byteCount) )
  1804.                             {
  1805.                                 result = ioErr;
  1806.                             }
  1807.                             startVerify += byteCount;
  1808.                             bytesVerified += byteCount;
  1809.                         }
  1810.                     }
  1811.                 }
  1812.             }
  1813.         }
  1814.         DisposePtr(verifyBuffer);
  1815.     }
  1816.     else
  1817.     {
  1818.         result = memFullErr;
  1819.     }
  1820.     return ( result );
  1821. }
  1822.  
  1823. /*****************************************************************************/
  1824.  
  1825. pascal    OSErr    CopyFork(short srcRefNum,
  1826.                          short dstRefNum,
  1827.                          void *copyBufferPtr,
  1828.                          long copyBufferSize)
  1829. {
  1830.     ParamBlockRec srcPB;
  1831.     ParamBlockRec dstPB;
  1832.     OSErr srcError;
  1833.     OSErr dstError;
  1834.  
  1835.     if ( (copyBufferPtr == NULL) || (copyBufferSize == 0) )
  1836.         return ( paramErr );
  1837.     
  1838.     srcPB.ioParam.ioRefNum = srcRefNum;
  1839.     dstPB.ioParam.ioRefNum = dstRefNum;
  1840.  
  1841.     /* preallocate the destination fork and */
  1842.     /* ensure the destination fork's EOF is correct after the copy */
  1843.     srcError = PBGetEOFSync(&srcPB);
  1844.     if ( srcError != noErr )
  1845.         return ( srcError );
  1846.     dstPB.ioParam.ioMisc = srcPB.ioParam.ioMisc;
  1847.     dstError = PBSetEOFSync(&dstPB);
  1848.     if ( dstError != noErr )
  1849.         return ( dstError );
  1850.  
  1851.     /* reset source fork's mark */
  1852.     srcPB.ioParam.ioPosMode = fsFromStart;
  1853.     srcPB.ioParam.ioPosOffset = 0;
  1854.     srcError = PBSetFPosSync(&srcPB);
  1855.     if ( srcError != noErr )
  1856.         return ( srcError );
  1857.  
  1858.     /* reset destination fork's mark */
  1859.     dstPB.ioParam.ioPosMode = fsFromStart;
  1860.     dstPB.ioParam.ioPosOffset = 0;
  1861.     dstError = PBSetFPosSync(&dstPB);
  1862.     if ( dstError != noErr )
  1863.         return ( dstError );
  1864.  
  1865.     /* set up fields that won't change in the loop */
  1866.     srcPB.ioParam.ioBuffer = (Ptr)copyBufferPtr;
  1867.     srcPB.ioParam.ioPosMode = fsAtMark + 0x0020;/* fsAtMark + noCacheBit */
  1868.     srcPB.ioParam.ioReqCount = ((copyBufferSize >= 512) && (copyBufferSize % 512)) ?
  1869.                                (copyBufferSize / 512) * 512 :
  1870.                                (copyBufferSize);
  1871.         /* If copyBufferSize is greater than 512 bytes, make it a multiple of 512 bytes */
  1872.         /* This will make writes on local volumes faster */
  1873.  
  1874.     dstPB.ioParam.ioBuffer = (Ptr)copyBufferPtr;
  1875.     dstPB.ioParam.ioPosMode = fsAtMark + 0x0020;/* fsAtMark + noCacheBit */
  1876.  
  1877.     while ( (srcError == noErr) && (dstError == noErr) )
  1878.     {
  1879.         srcError = PBReadSync(&srcPB);
  1880.         dstPB.ioParam.ioReqCount = srcPB.ioParam.ioActCount;
  1881.         dstError = PBWriteSync(&dstPB);
  1882.     }
  1883.  
  1884.     /* make sure there were no errors at the destination */
  1885.     if ( dstError != noErr )
  1886.         return ( dstError );
  1887.  
  1888.     /* make sure the only error at the source was eofErr */
  1889.     if ( srcError != eofErr )
  1890.         return ( srcError );
  1891.  
  1892.     return ( noErr );
  1893. }
  1894.  
  1895. /*****************************************************************************/
  1896.  
  1897. pascal    OSErr    GetFileLocation(short refNum,
  1898.                                 short *vRefNum,
  1899.                                 long *dirID,
  1900.                                 StringPtr fileName)
  1901. {
  1902.     FCBPBRec pb;
  1903.     OSErr error;
  1904.  
  1905.     pb.ioNamePtr = fileName;
  1906.     pb.ioVRefNum = 0;
  1907.     pb.ioRefNum = refNum;
  1908.     pb.ioFCBIndx = 0;
  1909.     error = PBGetFCBInfoSync(&pb);
  1910.     *vRefNum = pb.ioFCBVRefNum;
  1911.     *dirID = pb.ioFCBParID;
  1912.     return ( error );
  1913. }
  1914.  
  1915. /*****************************************************************************/
  1916.  
  1917. pascal    OSErr    FSpGetFileLocation(short refNum,
  1918.                                    FSSpec *spec)
  1919. {
  1920.     return ( GetFileLocation(refNum, &(spec->vRefNum), &(spec->parID), spec->name) );
  1921. }
  1922.  
  1923. /*****************************************************************************/
  1924.  
  1925. pascal    OSErr    CopyDirectoryAccess(short srcVRefNum,
  1926.                                     long srcDirID,
  1927.                                     StringPtr srcName,
  1928.                                     short dstVRefNum,
  1929.                                     long dstDirID,
  1930.                                     StringPtr dstName)
  1931. {    
  1932.     OSErr err;
  1933.     GetVolParmsInfoBuffer infoBuffer;    /* Where PBGetVolParms dumps its info */
  1934.     long    dstServerAdr;                /* AppleTalk server address of destination (if any) */
  1935.     Boolean    dstHasBlankAccessPrivileges;
  1936.     long    ownerID, groupID, accessRights;
  1937.     long    tempLong;
  1938.  
  1939.     /* See if destination supports directory access control */
  1940.     tempLong = sizeof(infoBuffer);
  1941.     err = HGetVolParms(dstName, dstVRefNum, &infoBuffer, &tempLong);
  1942.     if ( (err != noErr) && (err != paramErr) )
  1943.         return ( err );
  1944.     if ( (err != paramErr) && hasAccessCntl(infoBuffer) )
  1945.     {
  1946.         dstServerAdr = infoBuffer.vMServerAdr;
  1947.         dstHasBlankAccessPrivileges = hasBlankAccessPrivileges(infoBuffer);
  1948.     }
  1949.     else
  1950.         /* If destination doesn't support access privileges, */
  1951.         /* then there's nothing left to do here */
  1952.         return ( noErr );
  1953.  
  1954.     /* We may have to do something with access privileges at the destination. */
  1955.     
  1956.     accessRights = 0;    /* clear so we can tell if anything was done with this */
  1957.  
  1958.     /* See if source supports directory access control and is on same server */
  1959.     tempLong = sizeof(infoBuffer);
  1960.     err = HGetVolParms(srcName, srcVRefNum, &infoBuffer, &tempLong);
  1961.     if ( (err != noErr) && (err != paramErr) )
  1962.         return (err);
  1963.     if ( (err != paramErr) && hasAccessCntl(infoBuffer) )
  1964.     {
  1965.         /* Make sure both locations are on the same file server */
  1966.         if ( dstServerAdr == infoBuffer.vMServerAdr )
  1967.         {
  1968.             /* copy'm */
  1969.             err = HGetDirAccess(srcVRefNum, srcDirID, srcName, &ownerID, &groupID, &accessRights);
  1970.             if ( err == noErr )
  1971.                 err = HSetDirAccess(dstVRefNum, dstDirID, dstName, ownerID, groupID, accessRights);
  1972.         }
  1973.     }
  1974.     return ( err );
  1975. }
  1976.  
  1977. /*****************************************************************************/
  1978.  
  1979. pascal    OSErr    FSpCopyDirectoryAccess(const FSSpec *srcSpec,
  1980.                                        const FSSpec *dstSpec)
  1981. {
  1982.     return ( CopyDirectoryAccess(srcSpec->vRefNum, srcSpec->parID, (StringPtr)srcSpec->name,
  1983.                                 dstSpec->vRefNum, dstSpec->parID, (StringPtr)dstSpec->name) );
  1984. }
  1985.  
  1986. /*****************************************************************************/
  1987.  
  1988. pascal    OSErr    HMoveRenameCompat(short vRefNum,
  1989.                                   long srcDirID,
  1990.                                   ConstStr255Param srcName,
  1991.                                   long dstDirID,
  1992.                                   StringPtr dstpathName,
  1993.                                   StringPtr copyName)
  1994. {
  1995.     OSErr                    error;
  1996.     GetVolParmsInfoBuffer    volParmsInfo;
  1997.     long                    infoSize = sizeof(GetVolParmsInfoBuffer);
  1998.     short                    realVRefNum;
  1999.     long                    realParID;
  2000.     Str255                    realName;
  2001.     short                    tempItemsVRefNum;
  2002.     long                    tempItemsDirID;
  2003.     Boolean                    isDirectory;
  2004.     
  2005.     /* get volume attributes */
  2006.     error = HGetVolParms((StringPtr)srcName, vRefNum, &volParmsInfo, &infoSize);
  2007.     if ( error == noErr )
  2008.     {
  2009.         /* if volume supports move and rename, use it and return */
  2010.         if ( hasMoveRename(volParmsInfo) )
  2011.             return (HMoveRename(vRefNum, srcDirID, srcName, dstDirID, dstpathName, copyName));
  2012.     }
  2013.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  2014.         return ( error );
  2015.     
  2016.     /* MoveRename isn't supported by this volume, so do it by hand */
  2017.     
  2018.     /* if copyName isn't supplied, we can simply CatMove and return */
  2019.     if ( copyName == NULL )
  2020.         return ( CatMove(vRefNum, srcDirID, srcName, dstDirID, dstpathName) );
  2021.     
  2022.     /* renaming is required, so we have some work to do... */
  2023.     
  2024.     /* get the object's real name */
  2025.     error = GetObjectLocation(vRefNum, srcDirID, (StringPtr)srcName,
  2026.                                 &realVRefNum, &realParID, realName, &isDirectory);
  2027.     if ( error != noErr )
  2028.         return ( error );
  2029.     
  2030.     /* find temporary items folder */
  2031.     error = FindFolder(realVRefNum, kTemporaryFolderType, kCreateFolder,
  2032.                         &tempItemsVRefNum, &tempItemsDirID);
  2033.     if ( error != noErr )
  2034.         return ( error );
  2035.     
  2036.     /* move the object to a temporary items folder for renaming */
  2037.     error = CatMove(realVRefNum, realParID, realName, tempItemsDirID, NULL);
  2038.     if ( error != noErr )
  2039.         return ( error );
  2040.     
  2041.     /* rename the object */    
  2042.     error = HRename(tempItemsVRefNum, tempItemsDirID, realName, copyName);
  2043.     if ( error == noErr )
  2044.     {
  2045.         /* move object to its new home */
  2046.         error = CatMove(tempItemsVRefNum, tempItemsDirID, copyName, dstDirID, dstpathName);
  2047.         if ( error == noErr )
  2048.             return ( error );    /* IF NO ERRORS EXIT HERE! */
  2049.         
  2050.         /* Error handling: rename object back to original name - keep real error */
  2051.         (void) HRename(tempItemsVRefNum, tempItemsDirID, copyName, realName);
  2052.     }
  2053.     
  2054.     /* Error handling: move object back to original location - keep real error */
  2055.     (void) CatMove(tempItemsVRefNum, tempItemsDirID, realName, realParID, NULL);
  2056.     
  2057.     return ( error );
  2058. }
  2059.  
  2060. /*****************************************************************************/
  2061.  
  2062. pascal    OSErr    FSpMoveRenameCompat(const FSSpec *srcSpec,
  2063.                                     const FSSpec *dstSpec,
  2064.                                     StringPtr copyName)
  2065. {
  2066.     /* make sure the FSSpecs refer to the same volume */
  2067.     if (srcSpec->vRefNum != dstSpec->vRefNum)
  2068.         return (diffVolErr);
  2069.     return ( HMoveRenameCompat(srcSpec->vRefNum, srcSpec->parID, srcSpec->name,
  2070.                       dstSpec->parID, (StringPtr)dstSpec->name, copyName) );
  2071. }
  2072.  
  2073. /*****************************************************************************/
  2074.  
  2075. pascal    void    BuildAFPVolMountInfo(short theFlags,
  2076.                                      char theNBPInterval,
  2077.                                      char theNBPCount,
  2078.                                      short theUAMType,
  2079.                                      Str31 theZoneName,
  2080.                                      Str31 theServerName,
  2081.                                      Str27 theVolName,
  2082.                                      Str31 theUserName,
  2083.                                      Str8 theUserPassWord,
  2084.                                      Str8 theVolPassWord,
  2085.                                      MyAFPVolMountInfoPtr theAFPInfo)
  2086. {
  2087.     /* Fill in an AFPVolMountInfo record that can be passed to VolumeMount */
  2088.     theAFPInfo->length = sizeof(MyAFPVolMountInfo);
  2089.     theAFPInfo->media = AppleShareMediaType;
  2090.     theAFPInfo->flags = theFlags;
  2091.     theAFPInfo->nbpInterval = theNBPInterval;
  2092.     theAFPInfo->nbpCount = theNBPCount;
  2093.     theAFPInfo->uamType = theUAMType;
  2094.     theAFPInfo->zoneNameOffset = (short)((long)theAFPInfo->zoneName - (long)theAFPInfo);
  2095.     theAFPInfo->serverNameOffset = (short)((long)theAFPInfo->serverName - (long)theAFPInfo);
  2096.     theAFPInfo->volNameOffset = (short)((long)theAFPInfo->volName - (long)theAFPInfo);
  2097.     theAFPInfo->userNameOffset = (short)((long)theAFPInfo->userName - (long)theAFPInfo);
  2098.     theAFPInfo->userPasswordOffset = (short)((long)theAFPInfo->userPassword - (long)theAFPInfo);
  2099.     theAFPInfo->volPasswordOffset = (short)((long)theAFPInfo->volPassword - (long)theAFPInfo);
  2100.     
  2101.     BlockMoveData(theZoneName, theAFPInfo->zoneName, theZoneName[0] + 1);
  2102.     BlockMoveData(theServerName, theAFPInfo->serverName, theServerName[0] + 1);
  2103.     BlockMoveData(theVolName, theAFPInfo->volName, theVolName[0] + 1);
  2104.     BlockMoveData(theUserName, theAFPInfo->userName, theUserName[0] + 1);
  2105.     BlockMoveData(theUserPassWord, theAFPInfo->userPassword, theUserPassWord[0] + 1);
  2106.     BlockMoveData(theVolPassWord, theAFPInfo->volPassword, theVolPassWord[0] + 1);
  2107. }
  2108.  
  2109. /*****************************************************************************/
  2110.  
  2111. pascal    OSErr    RetrieveAFPVolMountInfo(AFPVolMountInfoPtr theAFPInfo,
  2112.                                         short *theFlags,
  2113.                                         short *theUAMType,
  2114.                                         StringPtr theZoneName,
  2115.                                         StringPtr theServerName,
  2116.                                         StringPtr theVolName,
  2117.                                         StringPtr theUserName)
  2118. {
  2119.     OSErr        error;
  2120.     StringPtr    tempPtr;
  2121.         
  2122.     /* Retrieve the AFP mounting information from an AFPVolMountInfo record. */
  2123.     if ( theAFPInfo->media == AppleShareMediaType )
  2124.     {
  2125.         *theFlags = theAFPInfo->flags;
  2126.         *theUAMType = theAFPInfo->uamType;
  2127.         
  2128.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->zoneNameOffset);
  2129.         BlockMoveData(tempPtr, theZoneName, tempPtr[0] + 1);
  2130.         
  2131.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->serverNameOffset);
  2132.         BlockMoveData(tempPtr, theServerName, tempPtr[0] + 1);
  2133.         
  2134.         tempPtr = (StringPtr)(StringPtr)((long)theAFPInfo + theAFPInfo->volNameOffset);
  2135.         BlockMoveData(tempPtr, theVolName, tempPtr[0] + 1);
  2136.         
  2137.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->userNameOffset);
  2138.         BlockMoveData(tempPtr, theUserName, tempPtr[0] + 1);
  2139.         
  2140.         error = noErr;
  2141.     }
  2142.     else
  2143.     {
  2144.         error = paramErr;
  2145.     }
  2146.     
  2147.     return ( error );
  2148. }
  2149.  
  2150. /*****************************************************************************/
  2151.  
  2152. pascal    OSErr    GetUGEntries(short objType,
  2153.                              UGEntryPtr entries,
  2154.                              long reqEntryCount,
  2155.                              long *actEntryCount,
  2156.                              long *objID)
  2157. {
  2158.     HParamBlockRec pb;
  2159.     OSErr error = noErr;
  2160.     UGEntry *endEntryArray = entries + reqEntryCount;
  2161.  
  2162.     pb.objParam.ioObjType = objType;
  2163.     *actEntryCount = 0;
  2164.     for ( ; (entries < endEntryArray) && (error == noErr); ++entries )
  2165.     {
  2166.         pb.objParam.ioObjNamePtr = (StringPtr)entries->name;
  2167.         pb.objParam.ioObjID = *objID;
  2168.         /* Files.h in the universal interfaces, PBGetUGEntrySync takes a CMovePBPtr */
  2169.         /* as the parameter. Inside Macintosh and the original glue used HParmBlkPtr. */
  2170.         /* A CMovePBPtr works OK, but this will be changed in the future  back to */
  2171.         /* HParmBlkPtr, so I'm just casting it here. */
  2172.         error = PBGetUGEntrySync((CMovePBPtr)&pb);
  2173.         if ( error == noErr )
  2174.         {
  2175.             entries->objID = *objID = pb.objParam.ioObjID;
  2176.             entries->objType = objType;
  2177.             ++*actEntryCount;
  2178.         }
  2179.     }
  2180.     return ( error );
  2181. }
  2182.